using System.Collections.Generic;
using System.Linq;
using System.Windows.Media;
using Caliburn.Micro;

namespace MediaOwl.Common.ScreenManagement
{
    public class Workspace<TDocument> : ExtendedConductor<TDocument>, IWorkspace
        where TDocument : class, IScreen
    {
        #region Fields
        protected internal IEnumerable<TDocument> ChildScreens;
        #endregion

        #region Implementation of IWorkspace

        private ImageBrush icon;
        public ImageBrush Icon
        {
            get { return icon; }
            protected set { icon = value; NotifyOfPropertyChange(() => Icon);}
        }

        private string iconName;
        public string IconName
        {
            get { return iconName; }
            protected set { iconName = value; NotifyOfPropertyChange(() => IconName); }
        }

        private int order;
        public int Order
        {
            get { return order; }
            protected set { order = value; NotifyOfPropertyChange(() => Order); }
        }

        #endregion

        protected override void OnActivate()
        {
            if (Items.Count == 0)
                FillItems();

            if (Items.Count > 0 && ActiveItem == null)
                ActivateItem(Items.FirstOrDefault());

            base.OnActivate();
        }

        protected internal void FillItems()
        {
            if (ChildScreens == null || ChildScreens.Count() == 0)
                return;

            Items.AddRange(ChildScreens.Where(x => x is IWorkitem && ((IWorkitem)x).IsHomeScreen).OrderBy(x => ((IWorkitem)x).Order));
        }
    }
}